package com.gamatechno.solodestination;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.apache.http.params.HttpAbstractParamBean;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.facebook.android.Facebook;
import com.facebook.android.SessionStore;
import com.gamatechno.adapter.JournalAdapter;
import com.gamatechno.solodestination.model.HttpUtils;
import com.gamatechno.solodestination.model.Journal;

import android.app.AlertDialog;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class JournalPerjalananFragment extends ListFragment {

	public final static String KEY_LOKASI = "Pasar Tumpah";
	public final static String KEY_KATEGORI = "Pasar Tradisional";
	public final static String KEY_TGL = "21";
	public final static String KEY_BLN = "JUNI";
	public final static String KEY_KOMEN = "Nasi gudeg";
	private static final String APP_ID = "239419142913008";
	private Facebook mFacebook;
	JournalAdapter adapter;
	String name;
	String id;

	public JournalPerjalananFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_journalperjalanan,
				container, false);

		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onActivityCreated(savedInstanceState);
		mFacebook = new Facebook(APP_ID);
		SessionStore.restore(mFacebook, getActivity());
		Log.wtf("mfacebook.isSessionValid()", mFacebook.isSessionValid() + "");
		if (mFacebook.isSessionValid()) {
			// TODO Auto-generated method stub
			try {

				String me = mFacebook.request("me");

				JSONObject jsonObj = (JSONObject) new JSONTokener(me)
						.nextValue();
				name = jsonObj.getString("name");
				id = jsonObj.getString("id");

				Log.i("fb", name);
				Log.i("fb", id);

				Toast.makeText(getActivity(), "Connect", Toast.LENGTH_SHORT)
						.show();
			} catch (Exception e) {
				// TODO: handle exception
			}
			Toast.makeText(getActivity(), "Connect", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(getActivity(), "Not Connect", Toast.LENGTH_SHORT)
					.show();
		}

		new GetJournal().execute();

	}

	private class GetJournal extends AsyncTask<Void, Void, ArrayList<Journal>> {
		ProgressDialog dialog = new ProgressDialog(getActivity());

		protected void onPreExecute() {
			dialog.setMessage(" Loading... ");
			dialog.show();
		}

		@Override
		protected ArrayList<Journal> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ArrayList<Journal> ajur = new ArrayList<Journal>();
			String url = "http://sms.gamatechno.com/solo_destination/services/getUser.php?method=journal&format=json&user_id="
					+ id + "&language=en";
			try {
				HttpUtils u = new HttpUtils();
				String a = u.getContents(url);
				JSONObject obj = new JSONObject(a);
				String jo = obj.get("result") + "";
				JSONArray ka = new JSONArray(jo);
				for (int i = 0; i < ka.length(); i++) {
					JSONObject ja = ka.getJSONObject(i);
					Journal jur = new Journal();
					jur.setCategory(ja.getString("category"));
					jur.setComment(ja.getString("comment"));
					jur.setName(ja.getString("name"));
					jur.setRating(ja.getString("rating"));
					jur.setDate(ja.getString("dateRate"));
					ajur.add(jur);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			return ajur;
		}

		@Override
		protected void onPostExecute(ArrayList<Journal> result) {
			// TODO Auto-generated method stub
			dialog.dismiss();
			try {
				setJournal(result);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		private void setJournal(ArrayList<Journal> result)
				throws ParseException {
			ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat newsdf = new SimpleDateFormat("yyyy-MMM-dd");
			for (Journal jurnal : result) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put(KEY_LOKASI, jurnal.getName());
				map.put(KEY_KATEGORI, jurnal.getCategory());
				Date date1 = sdf.parse(jurnal.getDate());
				String[] anu = newsdf.format(date1).split("-");
				map.put(KEY_TGL, anu[2]);
				map.put(KEY_BLN, anu[1]);
				map.put(KEY_KOMEN, jurnal.getComment());
				dataList.add(map);
				Log.d("anu", anu + "");
			}

			Log.d("DATA", dataList.toString());
			adapter = new JournalAdapter(getActivity(), dataList);
			getListView().setAdapter(adapter);

		}

	}

}
